<template>
  <Panel title="商品添加">
    <div slot="content">
      <el-form ref="form" :model="goodsForm" label-width="80px">
        <!-- 商品名称 -->
        <el-form-item label="商品名称">
          <el-input v-model="goodsForm.name"></el-input>
        </el-form-item>
        <!-- 商品分类 -->
        <el-form-item label="商品分类">
          <el-select v-model="goodsForm.category" placeholder="请选择分类">
            <el-option v-for="v in cates" :key="v" :label="v" :value="v"></el-option>
            
          </el-select>
        </el-form-item>
        <!-- 商品价格 -->
        <el-form-item label="商品价格">
          <template>
            <el-input-number
              v-model="goodsForm.price"
              :precision="2"
              :step="0.1"
              :min="0"
            ></el-input-number> 
            </template>      
         </el-form-item>
        <!-- 商品图片 -->
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="goodsForm.imgUrl" :src="goodsForm.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 商品描述 -->
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="goodsForm.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">添加商品</el-button>
        </el-form-item>
      </el-form>
    </div>
  </Panel>
</template>

<script>

import Panel from "@/components/Panel.vue";

import { queryGoodsCate,addGoods } from "@/api/user";
export default {
  components: {
    Panel,
  },
  data() {
    return {
      goodsForm: {
        name:"",
        category:"",
        price:"",
        imgUrl:"",
        goodsDesc:"",
        
      },
    //   分类类别
    cates:[],
       num: 10,
    };
  },
  created () {
    queryGoodsCate().then(res=>{
        // console.log(res);
        this.cates=res.data
    })
  },
  methods: {
 


     handleAvatarSuccess(res) {
                
                // console.log(res);
                if(res.code==0){
                    this.goodsForm.imgUrl = res.imgUrl
                }
                
            },
        beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG,PNG 格式!');
                }
                if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return (isJPG || isPNG) && isLt2M;

              
            },

    // 添加商品
    async onSubmit(){
        let res=await addGoods(this.goodsForm);
        console.log(res);
        if(res.data.code==0){
            this.$router.push("/goods/goodslist")
        }
    }

  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-input,
.el-textarea {
  width: 200px;
}
</style>